<?php
namespace Admin\Models\cost;

use Admin\Models\BaseModel;

class CostCharge extends BaseModel
{

    /**
     *
     * @var integer
     */
    public $id;

    /**
     *
     * @var integer
     */
    public $userId;

    /**
     *
     * @var string
     */
    public $year;

    /**
     *
     * @var double
     */
    public $receivable;

    /**
     *
     * @var double
     */
    public $received;

    /**
     *
     * @var integer
     */
    public $chargeNum;

    /**
     *
     * @var integer
     */
//    public $completeNum;

    /**
     *
     * @var integer
     */
    public $flowNum;

    /**
     *
     * @var integer
     */
    public $bDelete;

    /**
     *
     * @var string
     */
    public $tCreateDate;

    /**
     *
     * @var integer
     */
    public $iCreateUserId;

    /**
     *
     * @var string
     */
    public $tLastModifyDate;

    /**
     *
     * @var integer
     */
    public $iLastModifyUserId;

    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        parent::initialize();
    }

    /**
     * Returns table name mapped in the model.
     *
     * @return string
     */
    public function getSource()
    {
        return 'XHCost_Charge';
    }

    /**
     * Allows to query a set of records that match the specified conditions
     *
     * @param mixed $parameters
     * @return CostCharge[]|CostCharge|\Phalcon\Mvc\Model\ResultSetInterface
     */
    public static function find($parameters = null)
    {
        return parent::find($parameters);
    }

    /**
     * Allows to query the first record that match the specified conditions
     *
     * @param mixed $parameters
     * @return CostCharge|\Phalcon\Mvc\Model\ResultInterface
     */
    public static function findFirst($parameters = null)
    {
        return parent::findFirst($parameters);
    }

    /**
     * 获取初始化对象
     *
     * @param $year
     * @param $userId
     * @return CostCharge|\Phalcon\Mvc\Model\ResultInterface
     *
     * create by ck 20180914
     */
    public static function getInitObj($year, $userId)
    {
        $obj = self::findFirst([
            'conditions' => 'year = :year: AND userId = :userId: AND bDelete = 0',
            'bind' => ['year' => $year, 'userId' => $userId]
        ]);
        if (!$obj) {
            $obj = new self(['year' => $year, 'userId' => $userId]);
        }
        return $obj;
    }
}
